import 'package:flutter_admin/utils/utils.dart';

class MpsOrderLine {
  String? id;
  String? dateDemandStart;
  String? dateDemandEnd;
  String? origin;
  String? partnerName;
  String? productName;
  String? defaultCode;
  String? description;
  String? uom;
  num? priority;
  String? dateDemand;
  String? qtyOrder;
  String? available;
  String? state;

  MpsOrderLine({
    this.id,
    this.dateDemandStart,
    this.dateDemandEnd,
    this.origin,
    this.partnerName,
    this.productName,
    this.defaultCode,
    this.description,
    this.uom,
    this.priority,
    this.dateDemand,
    this.qtyOrder,
    this.available,
    this.state,
  });

  MpsOrderLine copyWith({
    String? id,
  }) {
    return new MpsOrderLine(
      id: id ?? this.id,
    );
  }

  @override
  String toString() {
    return '''MpsOrderLine{
      id: id,
    }''';
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      (other is MpsOrderLine &&
          runtimeType == other.runtimeType &&
          id == other.id);

  factory MpsOrderLine.fromMap(Map<String, dynamic> map) {
    return new MpsOrderLine(
      id: map['id'] != false ? map['id'].toString() as String? : "",
      origin: map['origin'] != false ? map['origin'].toString() as String? : "",
      partnerName: map['partner_id'] != false
          ? map['partner_id'][1].toString() as String?
          : "",
      description: map['description'] != false
          ? map['description'].toString() as String?
          : "",
      productName: map['product_name'] != false
          ? map['product_name'].toString() as String?
          : "",
      defaultCode: map['default_code'] != false
          ? map['default_code'].toString() as String?
          : "",
      uom: map['uom_id'] != false ? map['uom_id'][1].toString() as String? : "",
      priority: map['priority'] != false ? map['priority'] : 0,
      dateDemand: map['date_demand'] != false
          ? map['date_demand'].toString() as String?
          : "",
      qtyOrder: map['qty_order'] != false
          ? map['qty_order'].toString() as String?
          : "",
      available: map['available'] != false ? 'true' : 'false',
      state: map['state'] != false ? map['state'].toString() as String? : "",
    );
  }

  Map<String, dynamic> toMap() {
    // ignore: unnecessary_cast
    return {
      'id': this.id,
      'origin': this.origin,
      'partnerName': this.partnerName,
      'productName': this.productName,
      'defaultCode': this.defaultCode,
      'description': this.description,
      'uom': this.uom,
      'priority': this.priority,
      'dateDemand': this.dateDemand,
      'qtyOrder': this.qtyOrder,
      'available': this.available == 'true' ? '是' : '否',
      'state': this.state == 'draft'
          ? '新需求'
          : this.state == 'purchase'
              ? '采购中'
              : this.state == 'cancel'
                  ? '取消'
                  : '',
    } as Map<String, dynamic>;
  }

  List toDomainList() {
    List list = [
      if (this.dateDemandStart != null && this.dateDemandStart != '')
        [
          'date_demand',
          '>=',
          Utils.timeUTC(this.dateDemandStart! + " 00:00:00", isToBeijing: false)
        ],
      if (this.dateDemandEnd != null && this.dateDemandEnd != '')
        [
          'date_demand',
          '<=',
          Utils.timeUTC(this.dateDemandEnd! + " 23:59:59", isToBeijing: false)
        ],
      if (this.origin != null && this.origin != '')
        ['origin', 'like', '%${this.origin}%'],
      if (this.productName != null && this.productName != '')
        ['product_name', 'like', '%${this.productName}%'],
      if (this.defaultCode != null && this.defaultCode != '')
        ['default_code', 'like', '%${this.defaultCode}%'],
      if (this.available == 'true') ['available', '=', true],
      if (this.available == 'false') ['available', '=', false],
      (this.state != null && this.state != '')
          ? ['state', '=', this.state]
          : [
              'state',
              'in',
              ['draft', 'purchase']
            ],
    ] as List;
    // ignore: unnecessary_cast
    return list;
  }

  Map<String, dynamic> toOdooMap() {
    // ignore: unnecessary_cast
    return {
      'id': this.id,
    } as Map<String, dynamic>;
  }
  Map<String, dynamic> toPriorityMap() {
    // ignore: unnecessary_cast
    return {
      'id': this.id,
      'priority': this.priority,
    } as Map<String, dynamic>;
  }
}
